﻿Public Class HistoryDataClass

#Region "DealData"

    Public Shared Function DealData(ByVal dt As DateTime, ByVal buf() As Byte, ByVal ProjectID As String, ByVal netID As Byte) As Boolean

        If buf Is Nothing Then
            Exit Function
        End If

        If Not UtilityClass.BufferIsValidate(buf) Then
            Exit Function
        End If

        Dim tic As New TranstrationItem

        Dim strSql As String = String.Format("Insert Into tbccHDData_{0}_{1}(hDate,AI1,AI2,AI3,AI4,AI5,AI6,AI7,AI8,AI9,AI10,AI11,AI12," _
           & "Ref1_RefAlarmState,Ref1_LostPowerState,Ref1_SoundOutputState,Ref1_DoorState,Ref2_RefAlarmState,Ref2_LostPowerState,Ref2_SoundOutputState,Ref2_DoorState," _
           & "Ref3_RefAlarmState,Ref3_LostPowerState,Ref3_SoundOutputState,Ref3_DoorState,Ref4_RefAlarmState,Ref4_LostPowerState,Ref4_SoundOutputState,Ref4_DoorState,Uploadtime)" _
               & " Values(?p0,?p1,?p2,?p3,?p4,?p5,?p6,?p7,?p8,?p9,?p10,?p11,?p12,?p13,?p14,?p15,?p16,?p17,?p18,?p19,?p20,?p21,?p22,?p23,?p24,?p25,?p26,?p27,?p28,?p29) ; -- ", ProjectID.Replace("-", "_"), netID.ToString)


        For i As Integer = 0 To HIST_COUNT_EACH_PAGE - 1

            If UtilityClass.BufferIsValidate(buf, i * HIST_COUNT_EACH_TIME, HIST_COUNT_EACH_TIME) = False Then
                '此条记录的数据为全0或者全1 （255），视为无效数据
                Continue For
            End If

            Dim p(29) As MySql.Data.MySqlClient.MySqlParameter
            For pi As Integer = 0 To p.Length - 1
                p(pi) = New MySql.Data.MySqlClient.MySqlParameter()
                p(pi).ParameterName = "p" + pi.ToString

            Next

            p(0).Value = UtilityClass.GetDateString(dt.AddSeconds(i * HIST_SAVE_INTERVAL_TIME))


            '报警状态
            For j As Integer = 0 To 3  '各个冷库报警状态
                For k As Integer = 0 To 3  '当前冷库各个报警
                    p(13 + j * 4 + k).Value = GetBit(buf(i * HIST_COUNT_EACH_TIME + j), k).ToString
                Next
            Next

            '温度/湿度值
            For j As Integer = 0 To 11
                p(j + 1).Value = Int2Single(buf(i * HIST_COUNT_EACH_TIME + 2 * j + 4), buf(i * HIST_COUNT_EACH_TIME + 2 * j + 5)).ToString()
            Next

            tic.Add(strSql & i, p)
        Next

        '增加已上传报警库的标识信息

        If tic.Count > 0 Then
            strSql = String.Format("Insert Into tbccHistoryData_{0}(netID,iYear,iMonth,iDay,iHour,iMinute,iSecond,uploadTime) Values(?p0,?p1,?p2,?p3,?p4,?p5,?p6,?p7)", Trim(ProjectID).Replace("-", "_"))

            Dim p1(7) As MySql.Data.MySqlClient.MySqlParameter
            For i As Integer = 0 To p1.Length - 1
                p1(i) = New MySql.Data.MySqlClient.MySqlParameter()
            Next

            p1(0).ParameterName = "p0" : p1(0).Value = netID
            p1(1).ParameterName = "p1" : p1(1).Value = dt.Year
            p1(2).ParameterName = "p2" : p1(2).Value = dt.Month
            p1(3).ParameterName = "p3" : p1(3).Value = dt.Day
            p1(4).ParameterName = "p4" : p1(4).Value = dt.Hour
            p1(5).ParameterName = "p5" : p1(5).Value = dt.Minute
            p1(6).ParameterName = "p6" : p1(6).Value = dt.Second
            p1(7).ParameterName = "p7" : p1(7).Value = UtilityClass.GetDateString(Now)
            tic.Add(strSql, p1)

            Return SqlHelper.ExecuteTranstration(tic)

        End If

    End Function

#End Region


    '#Region "DealFinish"

    'Public Shared Function DealFinish(ByVal pid As String, ByVal dt As DateTime, ByVal netID As Byte) As Boolean
    '    Dim iYear As Integer
    '    Dim iMonth As Integer
    '    Dim iDay As Integer
    '    Dim iHour As Integer
    '    Dim iMinute As Integer
    '    Dim iSecond As Integer

    '    iYear = dt.Year
    '    iMonth = dt.Month
    '    iDay = dt.Day
    '    iHour = dt.Hour
    '    iMinute = dt.Minute
    '    iSecond = dt.Second

    '    Dim strSql As String
    '    strSql = String.Format("Insert Into tbccHistoryData_{0}(netID,iYear,iMonth,iDay,iHour,iMinute,iSecond,uploadTime) Values(?,?,?,?,?,?,?,?)", Trim(pid).Replace("-", "_"))

    '    Dim p(7) As MySql.Data.MySqlClient.MySqlParameter
    '    For i As Integer = 0 To p.Length - 1
    '        p(i) = New MySql.Data.MySqlClient.MySqlParameter()
    '    Next

    '    p(0).Value = netID
    '    p(1).Value = dt.Year
    '    p(2).Value = dt.Month
    '    p(3).Value = dt.Day
    '    p(4).Value = iHour
    '    p(5).Value = iMinute
    '    p(6).Value = iSecond
    '    p(7).Value = UtilityClass.GetDateString(Now)

    '    Dim icount As Integer
    '    icount = SqlHelper.ExecuteNonQuery(strSql, p)

    '    Return icount = 1
    'End Function

    '#End Region


    ''' <summary>
    ''' 温度的Int到Float的转换
    ''' </summary>
    ''' <param name="iValue"></param>
    ''' <returns></returns>
    ''' <remarks>将读到的Int值(2字节int)除以100得到对应的浮点值</remarks>
    Private Shared Function Int2Single(ByVal iValue As Short) As Single
        Return iValue * 0.01
    End Function

    Private Shared Function Int2Single(ByVal b1 As Byte, ByVal b2 As Byte) As Single
        Dim s As Int16
        Dim b(1) As Byte
        b(0) = b1
        b(1) = b2
        s = BitConverter.ToInt16(b, 0)
        Return s * 0.01
    End Function
    ''' <summary>
    ''' 存储数据页过程：（带有启停记录上传方式使用）
    ''' 数据插入数据库同时更新对应启停记录
    ''' </summary>
    ''' <param name="buf"></param>该页510byte内容
    ''' <param name="ProjectID"></param>工程ID
    ''' <param name="netID"></param>设备ID
    ''' <param name="startpage"></param>上传页号
    ''' <param name="HDsspage"></param>对应启停记录
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function DealData1(ByVal buf() As Byte, ByVal ProjectID As String, ByVal netID As Byte, ByVal startpage As Integer, ByVal HDsspage As HDStartStopItem) As Boolean
        Dim dt As DateTime
        Dim finish As Integer = 0
        Dim uploadtime As DateTime
        If buf Is Nothing Then

            Return False
        End If


        Dim strSql As String

        Dim tic As New TranstrationItem

        strSql = String.Format("Insert Into tbccHDData_{0}_{1}(hDate,AI1,AI2,AI3,AI4,AI5,AI6,AI7,AI8,AI9,AI10,AI11,AI12," _
           & "Ref1_RefAlarmState,Ref1_LostPowerState,Ref1_SoundOutputState,Ref1_DoorState,Ref1_ActiveState,Ref2_RefAlarmState,Ref2_LostPowerState,Ref2_SoundOutputState,Ref2_DoorState,Ref2_ActiveState," _
           & "Ref3_RefAlarmState,Ref3_LostPowerState,Ref3_SoundOutputState,Ref3_DoorState,Ref3_ActiveState,Ref4_RefAlarmState,Ref4_LostPowerState,Ref4_SoundOutputState,Ref4_DoorState,Ref4_ActiveState,Uploadtime,Ref5_DoorState,Ref6_DoorState,Ref7_DoorState,Ref8_DoorState)" _
               & " Values(?p0,?p1,?p2,?p3,?p4,?p5,?p6,?p7,?p8,?p9,?p10,?p11,?p12,?p13,?p14,?p15,?p16,?p17,?p18,?p19,?p20,?p21,?p22,?p23,?p24,?p25,?p26,?p27,?p28,?p29,?p30,?p31,?p32,?p33,?p34,?p35,?p36,?p37) ; -- ", ProjectID.Replace("-", "_"), netID.ToString)

        Dim n As Integer = 0
        Dim m As Integer = 0






        If startpage = HDsspage.UploadIndexPage Then  '上传页为上次上传结束页从结束页结束记录开始上传
            m = HDsspage.UploadIndexRecord
        End If


        For i As Integer = 0 To HIST_COUNT_PAGE - 1    '循环处理每条记录，每页最多存储12条记录
            'If startpage = HDsspage.UploadIndexPage AndAlso HDsspage.UploadIndexRecord = HIST_COUNT_PAGE Then  '该页已经写满
            '    Exit For
            '    'Return True
            'End If

            n = i + m                     '从上次结束记录开始上传
            If startpage = HDStartStopClass._nowPage Then   '写入页记录等于当前上传页记录退出循环
                If n > HDStartStopClass._index Then        '上传记录已经大于当前最后一条记录，退出上传循环
                    HDsspage.UploadIndexRecord = n
                    n -= 1
                    Exit For
                End If
            End If

            If n > HIST_COUNT_PAGE - 1 Then           '上传页大于11条记录，退出循环
                HDsspage.UploadIndexRecord = n
                Exit For
            End If
            If UtilityClass.BufferIsValidate(buf, n * HIST_COUNT_PAGE_BYTE, HIST_COUNT_PAGE_BYTE) = False Then
                '此条记录的数据为全0或者全1 （255），视为无效数据，退出循环，同时索引位置减1
                HDsspage.UploadIndexRecord = n
                n -= 1
                Exit For
            End If

            dt = New DateTime((buf(n * HIST_COUNT_PAGE_BYTE + 0) + 1900), buf(n * HIST_COUNT_PAGE_BYTE + 1), buf(n * HIST_COUNT_PAGE_BYTE + 2), buf(n * HIST_COUNT_PAGE_BYTE + 3), buf(n * HIST_COUNT_PAGE_BYTE + 4), buf(n * HIST_COUNT_PAGE_BYTE + 5))
            '该条记录时间
            HDsspage.UploadIndexRecord = n + 1

            Dim p(37) As MySql.Data.MySqlClient.MySqlParameter
            For pi As Integer = 0 To p.Length - 1
                p(pi) = New MySql.Data.MySqlClient.MySqlParameter()
                p(pi).ParameterName = "p" + pi.ToString
            Next

            p(0).Value = UtilityClass.GetDateString(dt)


            '报警状态
            For j As Integer = 0 To 3  '各个冷库报警状态


                '冷库预警或报警状态
                If 0 = GetBit(buf(n * HIST_COUNT_PAGE_BYTE + j + 6), 7) Then '冷库预警
                    p(13 + j * 5).Value = AlarmType.ALARMSTATE.PREALARM
                ElseIf 0 = GetBit(buf(n * HIST_COUNT_PAGE_BYTE + j + 6), 0) Then '冷库报警
                    p(13 + j * 5).Value = AlarmType.ALARMSTATE.ALARM
                Else
                    p(13 + j * 5).Value = AlarmType.ALARMSTATE.NORMAL
                End If

                p(13 + j * 5 + 1).Value = GetBit(buf(n * HIST_COUNT_PAGE_BYTE + j + 6), 1).ToString
                p(13 + j * 5 + 2).Value = GetBit(buf(n * HIST_COUNT_PAGE_BYTE + j + 6), 2).ToString
                'p(13 + j * 5 + 3).Value = IIf(GetBit(buf(n * HIST_COUNT_PAGE_BYTE + j + 6), 3) = 0, AlarmType.ALARMSTATE.NORMAL, AlarmType.ALARMSTATE.ALARM)
                p(13 + j * 5 + 3).Value = GetBit(buf(n * HIST_COUNT_PAGE_BYTE + j + 6), 3).ToString
                '冷库激活状态
                p(13 + j * 5 + 4).Value = GetBit(buf(n * HIST_COUNT_PAGE_BYTE + j + 6), 6).ToString
                ' p(13 + j * 4 + 3).Value = IIf(GetBit(buf(n * HIST_COUNT_PAGE_BYTE + j + 6), 5) = 1, ALARMSTATE.NORMAL, ALARMSTATE.ALARM)


            Next

            p(34).Value = GetBit(buf(n * HIST_COUNT_PAGE_BYTE + 7), 4).ToString  '1#冷库 库门2状态
            p(35).Value = GetBit(buf(n * HIST_COUNT_PAGE_BYTE + 7), 3).ToString  '2#冷库 库门2状态
            p(36).Value = GetBit(buf(n * HIST_COUNT_PAGE_BYTE + 8), 3).ToString  '3#冷库 库门2状态
            p(37).Value = GetBit(buf(n * HIST_COUNT_PAGE_BYTE + 9), 3).ToString  '4#冷库 库门2状态

            '温度/湿度值12个端口
            For j As Integer = 0 To 11
                p(j + 1).Value = Int2Single(buf(n * HIST_COUNT_PAGE_BYTE + 2 * j + 10), buf(n * HIST_COUNT_PAGE_BYTE + 2 * j + 11)).ToString()
            Next

            p(33).Value = UtilityClass.GetDateString(Now)


            tic.Add(strSql & i, p)
        Next

        '更新数据启停表信息，如果启停记录为停止，并且上传页为该启停记录最后一页，将该启停记录上传状态设置成完成
        If startpage = HDsspage.EndUploadPage AndAlso HDsspage.EndTime <> DateTime.MinValue Then
            finish = 1
        Else
            finish = 0

        End If

        If n = HIST_COUNT_PAGE Then  '将最后一条数据记录保存至启停记录

            uploadtime = New DateTime((buf((n - 1) * HIST_COUNT_PAGE_BYTE + 0) + 1900), buf((n - 1) * HIST_COUNT_PAGE_BYTE + 1), buf((n - 1) * HIST_COUNT_PAGE_BYTE + 2), buf((n - 1) * HIST_COUNT_PAGE_BYTE + 3), buf((n - 1) * HIST_COUNT_PAGE_BYTE + 4), buf((n - 1) * HIST_COUNT_PAGE_BYTE + 5))
        Else
            uploadtime = New DateTime((buf(n * HIST_COUNT_PAGE_BYTE + 0) + 1900), buf(n * HIST_COUNT_PAGE_BYTE + 1), buf(n * HIST_COUNT_PAGE_BYTE + 2), buf(n * HIST_COUNT_PAGE_BYTE + 3), buf(n * HIST_COUNT_PAGE_BYTE + 4), buf(n * HIST_COUNT_PAGE_BYTE + 5))



        End If

        '更新对应启停记录
        strSql = String.Format(" Update tbccHDDataStartEndPage set UploadIndexPage=?p0,UploadIndexRecord=?p1,UploadIndexRecordTime=?p2,Finished=?p3,EndTime=?p4 where netid=?p5 and starttime=?p6")

        Dim p1(6) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p1.Length - 1
            p1(i) = New MySql.Data.MySqlClient.MySqlParameter()
        Next

        p1(0).ParameterName = "p0" : p1(0).Value = startpage
        p1(1).ParameterName = "p1" : p1(1).Value = HDsspage.UploadIndexRecord
        p1(2).ParameterName = "p2" : p1(2).Value = UtilityClass.GetDateString(uploadtime)
        p1(3).ParameterName = "p3" : p1(3).Value = finish
        p1(4).ParameterName = "p4" : p1(4).Value = HDsspage.EndTime
        p1(5).ParameterName = "p5" : p1(5).Value = netID
        p1(6).ParameterName = "p6" : p1(6).Value = UtilityClass.GetDateString(HDsspage.StartTime)


        tic.Add(strSql, p1)
        Try
            Return SqlHelper.ExecuteTranstration(tic)

        Catch ex As MySql.Data.MySqlClient.MySqlException
            Return False

        End Try



    End Function
    ''' <summary>
    ''' 存储数据页过程：（带有启停记录上传方式使用）
    ''' 数据插入数据库同时更新对应启停记录
    ''' </summary>
    ''' <param name="buf"></param>该页510byte内容
    ''' <param name="ProjectID"></param>工程ID
    ''' <param name="netID"></param>设备ID
    ''' <param name="startpage"></param>上传页号
    ''' <param name="HDsspage"></param>对应启停记录
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function DealDataEx16(ByVal buf() As Byte, ByVal ProjectID As String, ByVal netID As Byte, ByVal startpage As Integer, ByVal HDsspage As HDStartStopItem) As Boolean
        Dim dt As DateTime
        Dim finish As Integer = 0
        Dim uploadtime As DateTime
        If buf Is Nothing Then

            Return False
        End If


        Dim strSql As String

        Dim tic As New TranstrationItem

        strSql = String.Format("Insert Into tbccHDData_{0}_{1}(hDate,AI1,AI2,AI3,AI4,AI5,AI6,AI7,AI8,AI9,AI10,AI11,AI12,AI13,AI14,AI15,AI16,AI17,AI18,AI19,AI20,AI21,AI22,AI23,AI24,AI25,AI26,AI27,AI28,AI29,AI30,AI31,AI32," _
           & "Ref1_LostPowerState,Ref1_SoundOutputState,Ref1_RefAlarmState,Ref1_ActiveState,Ref2_RefAlarmState,Ref2_ActiveState,Ref3_RefAlarmState,Ref3_ActiveState,Ref4_RefAlarmState,Ref4_ActiveState,Ref5_RefAlarmState,Ref5_ActiveState,Ref6_RefAlarmState,Ref6_ActiveState,Ref7_RefAlarmState,Ref7_ActiveState," _
           & "Ref8_RefAlarmState,Ref8_ActiveState,Ref9_RefAlarmState,Ref9_ActiveState,Ref10_RefAlarmState,Ref10_ActiveState,Ref11_RefAlarmState,Ref11_ActiveState,Ref12_RefAlarmState,Ref12_ActiveState,Ref13_RefAlarmState,Ref13_ActiveState,Ref14_RefAlarmState,Ref14_ActiveState,Ref15_RefAlarmState,Ref15_ActiveState,Ref16_RefAlarmState,Ref16_ActiveState,Uploadtime)" _
               & " Values(?p0,?p1,?p2,?p3,?p4,?p5,?p6,?p7,?p8,?p9,?p10,?p11,?p12,?p13,?p14,?p15,?p16,?p17,?p18,?p19,?p20,?p21,?p22,?p23,?p24,?p25,?p26,?p27,?p28,?p29,?p30,?p31,?p32,?p33,?p34,?p35,?p36,?p37,?p38,?p39,?p40,?p41,?p42,?p43,?p44,?p45,?p46,?p47,?p48," _
            & "?p49,?p50,?p51,?p52,?p53,?p54,?p55,?p56,?p57,?p58,?p59,?p60,?p61,?p62,?p63,?p64,?p65,?p66,?p67) ; -- ", ProjectID.Replace("-", "_"), netID.ToString)

        Dim n As Integer = 0
        Dim m As Integer = 0






        If startpage = HDsspage.UploadIndexPage Then  '上传页为上次上传结束页从结束页结束记录开始上传
            m = HDsspage.UploadIndexRecord
        End If


        For i As Integer = 0 To HIST_COUNT_PAGE_EX16 - 1    '循环处理每条记录，每页最多存储6条记录
            'If startpage = HDsspage.UploadIndexPage AndAlso HDsspage.UploadIndexRecord = HIST_COUNT_PAGE Then  '该页已经写满
            '    Exit For
            '    'Return True
            'End If

            n = i + m                     '从上次结束记录开始上传
            If startpage = HDStartStopClass._nowPage Then   '写入页记录等于当前上传页记录退出循环
                If n > HDStartStopClass._index Then        '上传记录已经大于当前最后一条记录，退出上传循环
                    HDsspage.UploadIndexRecord = n
                    n -= 1
                    Exit For
                End If
            End If

            If n > HIST_COUNT_PAGE_EX16 - 1 Then           '上传页大于6条记录，退出循环
                HDsspage.UploadIndexRecord = n
                Exit For
            End If
            If UtilityClass.BufferIsValidate(buf, n * HIST_COUNT_PAGE_BYTE_EX16, HIST_COUNT_PAGE_BYTE_EX16) = False Then
                '此条记录的数据为全0或者全1 （255），视为无效数据，退出循环，同时索引位置减1
                HDsspage.UploadIndexRecord = n
                n -= 1
                Exit For
            End If

            dt = New DateTime((buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 0) + 1900), buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 1), buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 2), buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 3), buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 4), buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 5))
            '该条记录时间
            HDsspage.UploadIndexRecord = n + 1

            Dim p(67) As MySql.Data.MySqlClient.MySqlParameter
            For pi As Integer = 0 To p.Length - 1
                p(pi) = New MySql.Data.MySqlClient.MySqlParameter()
                p(pi).ParameterName = "p" + pi.ToString
            Next

            p(0).Value = UtilityClass.GetDateString(dt)


            p(33).Value = GetBit(buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 6), 0)
            p(34).Value = GetBit(buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 6), 2)
            '报警状态
            For j As Integer = 0 To 15  '各个冷库报警状态


                If j Mod 2 = 0 Then
                    '冷库预警或报警状态
                    If 0 = GetBit(buf(n * HIST_COUNT_PAGE_BYTE_EX16 + Math.Truncate(j / 2) + 8), 0) Then '冷库预警
                        p(35 + j * 2).Value = AlarmType.ALARMSTATE.PREALARM
                    ElseIf 0 = GetBit(buf(n * HIST_COUNT_PAGE_BYTE_EX16 + Math.Truncate(j / 2) + 8), 1) Then '冷库报警
                        p(35 + j * 2).Value = AlarmType.ALARMSTATE.ALARM
                    Else
                        p(35 + j * 2).Value = AlarmType.ALARMSTATE.NORMAL
                    End If

                    '冷库激活状态
                    p(36 + j * 2).Value = GetBit(buf(n * HIST_COUNT_PAGE_BYTE_EX16 + Math.Truncate(j / 2) + 8), 2).ToString
                Else
                    '冷库预警或报警状态
                    If 0 = GetBit(buf(n * HIST_COUNT_PAGE_BYTE_EX16 + Math.Truncate(j / 2) + 8), 4) Then '冷库预警
                        p(35 + j * 2).Value = AlarmType.ALARMSTATE.PREALARM
                    ElseIf 0 = GetBit(buf(n * HIST_COUNT_PAGE_BYTE_EX16 + Math.Truncate(j / 2) + 8), 5) Then '冷库报警
                        p(35 + j * 2).Value = AlarmType.ALARMSTATE.ALARM
                    Else
                        p(35 + j * 2).Value = AlarmType.ALARMSTATE.NORMAL
                    End If

                    '冷库激活状态
                    p(36 + j * 2).Value = GetBit(buf(n * HIST_COUNT_PAGE_BYTE_EX16 + Math.Truncate(j / 2) + 8), 6).ToString

                End If
               



            Next

            '温度/湿度值32个端口
            For j As Integer = 0 To 31
                p(j + 1).Value = Int2Single(buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 2 * j + 16), buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 2 * j + 17)).ToString()
            Next

            p(67).Value = UtilityClass.GetDateString(Now)


            tic.Add(strSql & i, p)
        Next

        '更新数据启停表信息，如果启停记录为停止，并且上传页为该启停记录最后一页，将该启停记录上传状态设置成完成
        If startpage = HDsspage.EndUploadPage AndAlso HDsspage.EndTime <> DateTime.MinValue Then
            finish = 1
        Else
            finish = 0

        End If

        If n = HIST_COUNT_PAGE_EX16 Then  '将最后一条数据记录保存至启停记录

            uploadtime = New DateTime((buf((n - 1) * HIST_COUNT_PAGE_BYTE_EX16 + 0) + 1900), buf((n - 1) * HIST_COUNT_PAGE_BYTE_EX16 + 1), buf((n - 1) * HIST_COUNT_PAGE_BYTE_EX16 + 2), buf((n - 1) * HIST_COUNT_PAGE_BYTE_EX16 + 3), buf((n - 1) * HIST_COUNT_PAGE_BYTE_EX16 + 4), buf((n - 1) * HIST_COUNT_PAGE_BYTE_EX16 + 5))
        Else
            uploadtime = New DateTime((buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 0) + 1900), buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 1), buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 2), buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 3), buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 4), buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 5))



        End If

        '更新对应启停记录
        strSql = String.Format(" Update tbccHDDataStartEndPage set UploadIndexPage=?p0,UploadIndexRecord=?p1,UploadIndexRecordTime=?p2,Finished=?p3,EndTime=?p4 where netid=?p5 and starttime=?p6")

        Dim p1(6) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p1.Length - 1
            p1(i) = New MySql.Data.MySqlClient.MySqlParameter()
        Next

        p1(0).ParameterName = "p0" : p1(0).Value = startpage
        p1(1).ParameterName = "p1" : p1(1).Value = HDsspage.UploadIndexRecord
        p1(2).ParameterName = "p2" : p1(2).Value = UtilityClass.GetDateString(uploadtime)
        p1(3).ParameterName = "p3" : p1(3).Value = finish
        p1(4).ParameterName = "p4" : p1(4).Value = HDsspage.EndTime
        p1(5).ParameterName = "p5" : p1(5).Value = netID
        p1(6).ParameterName = "p6" : p1(6).Value = UtilityClass.GetDateString(HDsspage.StartTime)


        tic.Add(strSql, p1)

        Return SqlHelper.ExecuteTranstration(tic)

    End Function


    Public Shared Function DealData2(ByVal buf() As Byte, ByVal ProjectID As String, ByVal netID As Byte, ByVal startpage As Integer, ByVal dt1 As DateTime) As Boolean
        Dim dt As DateTime

        Dim uploadtime As DateTime
        If buf Is Nothing Then
            Return False
        End If


        Dim strSql As String

        Dim tic As New TranstrationItem

        strSql = String.Format("Insert Into tbccHDData_{0}_{1}(hDate,AI1,AI2,AI3,AI4,AI5,AI6,AI7,AI8,AI9,AI10,AI11,AI12," _
            & "Ref1_RefAlarmState,Ref1_LostPowerState,Ref1_SoundOutputState,Ref1_DoorState,Ref1_ActiveState,Ref2_RefAlarmState,Ref2_LostPowerState,Ref2_SoundOutputState,Ref2_DoorState,Ref2_ActiveState," _
            & "Ref3_RefAlarmState,Ref3_LostPowerState,Ref3_SoundOutputState,Ref3_DoorState,Ref3_ActiveState,Ref4_RefAlarmState,Ref4_LostPowerState,Ref4_SoundOutputState,Ref4_DoorState,Ref4_ActiveState,Uploadtime,Ref5_DoorState,Ref6_DoorState,Ref7_DoorState,Ref8_DoorState)" _
                & " Values(?p0,?p1,?p2,?p3,?p4,?p5,?p6,?p7,?p8,?p9,?p10,?p11,?p12,?p13,?p14,?p15,?p16,?p17,?p18,?p19,?p20,?p21,?p22,?p23,?p24,?p25,?p26,?p27,?p28,?p29,?p30,?p31,?p32,?p33,?p34,?p35,?p36,?p37) ; -- ", ProjectID.Replace("-", "_"), netID.ToString)

        Dim n As Integer = 0

        For i As Integer = 0 To HIST_COUNT_PAGE - 1

            n = i
            If UtilityClass.BufferIsValidate(buf, n * HIST_COUNT_PAGE_BYTE, HIST_COUNT_PAGE_BYTE) = False Then
                '此条记录的数据为全0或者全1 （255），视为无效数据

                Exit For
            End If

            dt = New DateTime((buf(n * HIST_COUNT_PAGE_BYTE + 0) + 1900), buf(n * HIST_COUNT_PAGE_BYTE + 1), buf(n * HIST_COUNT_PAGE_BYTE + 2), buf(n * HIST_COUNT_PAGE_BYTE + 3), buf(n * HIST_COUNT_PAGE_BYTE + 4), buf(n * HIST_COUNT_PAGE_BYTE + 5))


            Dim p(37) As MySql.Data.MySqlClient.MySqlParameter
            For pi As Integer = 0 To p.Length - 1
                p(pi) = New MySql.Data.MySqlClient.MySqlParameter()
                p(pi).ParameterName = "p" + pi.ToString
            Next

            p(0).Value = UtilityClass.GetDateString(dt)

            '报警状态
            For j As Integer = 0 To 3  '各个冷库报警状态
                'For k As Integer = 0 To 3  '当前冷库各个报警
                '    p(13 + j * 5 + k).Value = GetBit(buf(n * HIST_COUNT_PAGE_BYTE + j + 6), k).ToString
                'Next

                '冷库预警或报警状态
                If 0 = GetBit(buf(n * HIST_COUNT_PAGE_BYTE + j + 6), 7) Then '冷库预警
                    p(13 + j * 5).Value = AlarmType.ALARMSTATE.PREALARM
                ElseIf 0 = GetBit(buf(n * HIST_COUNT_PAGE_BYTE + j + 6), 0) Then '冷库报警
                    p(13 + j * 5).Value = AlarmType.ALARMSTATE.ALARM
                Else
                    p(13 + j * 5).Value = AlarmType.ALARMSTATE.NORMAL
                End If

                p(13 + j * 5 + 1).Value = GetBit(buf(n * HIST_COUNT_PAGE_BYTE + j + 6), 1).ToString
                p(13 + j * 5 + 2).Value = GetBit(buf(n * HIST_COUNT_PAGE_BYTE + j + 6), 2).ToString
                p(13 + j * 5 + 3).Value = GetBit(buf(n * HIST_COUNT_PAGE_BYTE + j + 6), 3).ToString

                '冷库激活状态
                p(13 + j * 5 + 4).Value = GetBit(buf(n * HIST_COUNT_PAGE_BYTE + j + 6), 6).ToString
            Next

            p(34).Value = GetBit(buf(n * HIST_COUNT_PAGE_BYTE + 7), 4).ToString  '1#冷库 库门2状态
            p(35).Value = GetBit(buf(n * HIST_COUNT_PAGE_BYTE + 7), 3).ToString  '2#冷库 库门2状态
            p(36).Value = GetBit(buf(n * HIST_COUNT_PAGE_BYTE + 8), 3).ToString  '3#冷库 库门2状态
            p(37).Value = GetBit(buf(n * HIST_COUNT_PAGE_BYTE + 9), 3).ToString  '4#冷库 库门2状态

            '温度/湿度值
            For j As Integer = 0 To 11
                p(j + 1).Value = Int2Single(buf(n * HIST_COUNT_PAGE_BYTE + 2 * j + 10), buf(n * HIST_COUNT_PAGE_BYTE + 2 * j + 11)).ToString()
            Next

            p(33).Value = UtilityClass.GetDateString(Now)

            tic.Add(strSql & i, p)
        Next

        '更新历史数据上传表信息        

        If n = 0 Then
        Else
            If buf((n - 1) * HIST_COUNT_PAGE_BYTE) = 0 Then
                uploadtime = New DateTime(1, 1, 1, 1, 1, 1)
            Else
                If n = HIST_COUNT_PAGE - 1 Then
                    uploadtime = New DateTime((buf(n * HIST_COUNT_PAGE_BYTE + 0) + 1900), buf(n * HIST_COUNT_PAGE_BYTE + 1), buf(n * HIST_COUNT_PAGE_BYTE + 2), buf(n * HIST_COUNT_PAGE_BYTE + 3), buf(n * HIST_COUNT_PAGE_BYTE + 4), buf(n * HIST_COUNT_PAGE_BYTE + 5))

                Else
                    uploadtime = New DateTime((buf((n - 1) * HIST_COUNT_PAGE_BYTE + 0) + 1900), buf((n - 1) * HIST_COUNT_PAGE_BYTE + 1), buf((n - 1) * HIST_COUNT_PAGE_BYTE + 2), buf((n - 1) * HIST_COUNT_PAGE_BYTE + 3), buf((n - 1) * HIST_COUNT_PAGE_BYTE + 4), buf((n - 1) * HIST_COUNT_PAGE_BYTE + 5))


                End If

            End If

        End If


        strSql = String.Format(" Update tbccHDManualUploadRecord set endtime=?p0,EndPage=?p1 where netid=?p2 and starttime=?p3")

        Dim p1(3) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p1.Length - 1
            p1(i) = New MySql.Data.MySqlClient.MySqlParameter()
        Next

        p1(0).ParameterName = "p0" : p1(0).Value = uploadtime
        p1(1).ParameterName = "p1" : p1(1).Value = startpage
        p1(2).ParameterName = "p2" : p1(2).Value = netID
        p1(3).ParameterName = "p3" : p1(3).Value = UtilityClass.GetDateString(dt1)

        tic.Add(strSql, p1)

        Return SqlHelper.ExecuteTranstration(tic)



        'Return True
    End Function
    Public Shared Function DealData2Ex16(ByVal buf() As Byte, ByVal ProjectID As String, ByVal netID As Byte, ByVal startpage As Integer, ByVal dt1 As DateTime) As Boolean
        Dim dt As DateTime

        Dim uploadtime As DateTime
        If buf Is Nothing Then
            Return False
        End If


        Dim strSql As String

        Dim tic As New TranstrationItem

        strSql = String.Format("Insert Into tbccHDData_{0}_{1}(hDate,AI1,AI2,AI3,AI4,AI5,AI6,AI7,AI8,AI9,AI10,AI11,AI12,AI13,AI14,AI15,AI16,AI17,AI18,AI19,AI20,AI21,AI22,AI23,AI24,AI25,AI26,AI27,AI28,AI29,AI30,AI31,AI32," _
           & "Ref1_LostPowerState,Ref1_SoundOutputState,Ref1_RefAlarmState,Ref1_ActiveState,Ref2_RefAlarmState,Ref2_ActiveState,Ref3_RefAlarmState,Ref3_ActiveState,Ref4_RefAlarmState,Ref4_ActiveState,Ref5_RefAlarmState,Ref5_ActiveState,Ref6_RefAlarmState,Ref6_ActiveState,Ref7_RefAlarmState,Ref7_ActiveState," _
           & "Ref8_RefAlarmState,Ref8_ActiveState,Ref9_RefAlarmState,Ref9_ActiveState,Ref10_RefAlarmState,Ref10_ActiveState,Ref11_RefAlarmState,Ref11_ActiveState,Ref12_RefAlarmState,Ref12_ActiveState,Ref13_RefAlarmState,Ref13_ActiveState,Ref14_RefAlarmState,Ref14_ActiveState,Ref15_RefAlarmState,Ref15_ActiveState,Ref16_RefAlarmState,Ref16_ActiveState,Uploadtime)" _
               & " Values(?p0,?p1,?p2,?p3,?p4,?p5,?p6,?p7,?p8,?p9,?p10,?p11,?p12,?p13,?p14,?p15,?p16,?p17,?p18,?p19,?p20,?p21,?p22,?p23,?p24,?p25,?p26,?p27,?p28,?p29,?p30,?p31,?p32,?p33,?p34,?p35,?p36,?p37,?p38,?p39,?p40,?p41,?p42,?p43,?p44,?p45,?p46,?p47,?p48," _
            & "?p49,?p50,?p51,?p52,?p53,?p54,?p55,?p56,?p57,?p58,?p59,?p60,?p61,?p62,?p63,?p64,?p65,?p66,?p67) ; -- ", ProjectID.Replace("-", "_"), netID.ToString)

        Dim n As Integer = 0

        For i As Integer = 0 To HIST_COUNT_PAGE_EX16 - 1    '循环处理每条记录，每页最多存储6条记录
            'If startpage = HDsspage.UploadIndexPage AndAlso HDsspage.UploadIndexRecord = HIST_COUNT_PAGE Then  '该页已经写满
            '    Exit For
            '    'Return True
            'End If

            n = i                '从上次结束记录开始上传
            
            If UtilityClass.BufferIsValidate(buf, n * HIST_COUNT_PAGE_BYTE_EX16, HIST_COUNT_PAGE_BYTE_EX16) = False Then
                '此条记录的数据为全0或者全1 （255），视为无效数据，退出循环，同时索引位置减1
               
                Exit For
            End If

            dt = New DateTime((buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 0) + 1900), buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 1), buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 2), buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 3), buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 4), buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 5))
           

            Dim p(67) As MySql.Data.MySqlClient.MySqlParameter
            For pi As Integer = 0 To p.Length - 1
                p(pi) = New MySql.Data.MySqlClient.MySqlParameter()
                p(pi).ParameterName = "p" + pi.ToString
            Next

            p(0).Value = UtilityClass.GetDateString(dt)


            p(33).Value = GetBit(buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 6), 0)
            p(34).Value = GetBit(buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 6), 2)
            '报警状态
            For j As Integer = 0 To 15  '各个冷库报警状态


                If j Mod 2 = 0 Then
                    '冷库预警或报警状态
                    If 0 = GetBit(buf(n * HIST_COUNT_PAGE_BYTE_EX16 + Math.Truncate(j / 2) + 8), 0) Then '冷库预警
                        p(35 + j * 2).Value = AlarmType.ALARMSTATE.PREALARM
                    ElseIf 0 = GetBit(buf(n * HIST_COUNT_PAGE_BYTE_EX16 + Math.Truncate(j / 2) + 8), 1) Then '冷库报警
                        p(35 + j * 2).Value = AlarmType.ALARMSTATE.ALARM
                    Else
                        p(35 + j * 2).Value = AlarmType.ALARMSTATE.NORMAL
                    End If

                    '冷库激活状态
                    p(36 + j * 2).Value = GetBit(buf(n * HIST_COUNT_PAGE_BYTE_EX16 + Math.Truncate(j / 2) + 8), 2).ToString
                Else
                    '冷库预警或报警状态
                    If 0 = GetBit(buf(n * HIST_COUNT_PAGE_BYTE_EX16 + Math.Truncate(j / 2) + 8), 4) Then '冷库预警
                        p(35 + j * 2).Value = AlarmType.ALARMSTATE.PREALARM
                    ElseIf 0 = GetBit(buf(n * HIST_COUNT_PAGE_BYTE_EX16 + Math.Truncate(j / 2) + 8), 5) Then '冷库报警
                        p(35 + j * 2).Value = AlarmType.ALARMSTATE.ALARM
                    Else
                        p(35 + j * 2).Value = AlarmType.ALARMSTATE.NORMAL
                    End If

                    '冷库激活状态
                    p(36 + j * 2).Value = GetBit(buf(n * HIST_COUNT_PAGE_BYTE_EX16 + Math.Truncate(j / 2) + 8), 6).ToString

                End If




            Next

            '温度/湿度值32个端口
            For j As Integer = 0 To 31
                p(j + 1).Value = Int2Single(buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 2 * j + 16), buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 2 * j + 17)).ToString()
            Next

            p(67).Value = UtilityClass.GetDateString(Now)


            tic.Add(strSql & i, p)
        Next

        '更新历史数据上传表信息        

        If n = 0 Then
        Else
            If buf((n - 1) * HIST_COUNT_PAGE_BYTE_EX16) = 0 Then
                uploadtime = New DateTime(1, 1, 1, 1, 1, 1)
            Else
                If n = HIST_COUNT_PAGE_EX16 - 1 Then
                    uploadtime = New DateTime((buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 0) + 1900), buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 1), buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 2), buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 3), buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 4), buf(n * HIST_COUNT_PAGE_BYTE_EX16 + 5))

                Else
                    uploadtime = New DateTime((buf((n - 1) * HIST_COUNT_PAGE_BYTE_EX16 + 0) + 1900), buf((n - 1) * HIST_COUNT_PAGE_BYTE_EX16 + 1), buf((n - 1) * HIST_COUNT_PAGE_BYTE_EX16 + 2), buf((n - 1) * HIST_COUNT_PAGE_BYTE_EX16 + 3), buf((n - 1) * HIST_COUNT_PAGE_BYTE_EX16 + 4), buf((n - 1) * HIST_COUNT_PAGE_BYTE_EX16 + 5))


                End If

            End If

        End If


        strSql = String.Format(" Update tbccHDManualUploadRecord set endtime=?p0,EndPage=?p1 where netid=?p2 and starttime=?p3")

        Dim p1(3) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p1.Length - 1
            p1(i) = New MySql.Data.MySqlClient.MySqlParameter()
        Next

        p1(0).ParameterName = "p0" : p1(0).Value = uploadtime
        p1(1).ParameterName = "p1" : p1(1).Value = startpage
        p1(2).ParameterName = "p2" : p1(2).Value = netID
        p1(3).ParameterName = "p3" : p1(3).Value = UtilityClass.GetDateString(dt1)

        tic.Add(strSql, p1)

        Return SqlHelper.ExecuteTranstration(tic)



        'Return True
    End Function

 
    Public Shared Function GetAvlidRecord(ByVal pid As String, ByVal netid As Integer, ByVal startTime As DateTime, ByVal dt2 As DateTime) As ArrayList   '返回有效手动上传数据列表
        Dim allAl As DataSet = Nothing
        Dim StartPageList As New ArrayList
        Dim EndPageList As New ArrayList
        Dim AllReUploadpage As New ArrayList
        Dim strSql1 As String = String.Format("SELECT StartPage,EndPage FROM tbccHDManualUploadRecord WHERE netID='{0}' AND  valid='{1}' and projectid='{2}'and StartTime <='{4}' and endtime >='{3}'", netid, 0, pid, startTime, dt2)

        allAl = SqlHelper.ExecuteDataset(strSql1, Nothing)
        If allAl.Tables.Count = 0 OrElse allAl.Tables(0).Rows.Count = 0 Then
            allAl.Clear() : allAl.Dispose() : allAl = Nothing
            Return Nothing
        End If


        For Each i As DataRow In allAl.Tables(0).Rows
            If i("endpage") = -1 Then   '新插入的手动记录不做过滤
                Continue For
            End If

            EndPageList.Add(i("endpage"))
            StartPageList.Add(i("StartPage"))
        Next

        For k As Integer = 0 To EndPageList.Count - 1
            For i As Integer = 0 To HIST_ALL_PAGE - 1
                Dim page As Integer
                page = (StartPageList.Item(k) + i) Mod HIST_ALL_PAGE
                If AllReUploadpage.Contains(page) Then
                Else
                    AllReUploadpage.Add(page)

                End If
                If page = EndPageList.Item(k) Then
                    Exit For
                End If

            Next

        Next

        Return AllReUploadpage

    End Function
    ''' <summary>
    ''' 判断指定位是否为1
    ''' </summary>
    ''' <param name="b"></param>
    ''' <param name="i"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Shared Function GetBit(ByVal b As Byte, ByVal i As Integer) As Byte
        Dim iTest As Byte
        iTest = 2 ^ i

        Dim result As Byte
        result = b And iTest

        If result > 0 Then
            result = 1
        End If

        Return result
    End Function


    ''' <summary>
    ''' 温度的Int到Float的转换
    ''' </summary>
    ''' <param name="iValue"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Shared Function Temperature_Int2Single(ByVal iValue As UShort) As Single
        Return iValue
    End Function

End Class
